<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="/static/layui/css/layui.css">
  <title>Document</title>
  <style>
    .layui-container {
      margin-top: 20px;
    }
  </style>
</head>

<body>
  <div>
    <span class="layui-breadcrumb">
      <a href="/dashboard">仪表盘</a>
      <a href="/itemlist">商品列表</a>
      <a><cite>添加商品</cite></a>
    </span>
  </div>
  <div class="layui-container">
    <form class="layui-form layui-form-pane">
      <!-- 商品名称 -->
      <div class="layui-form-item">
        <label class="layui-form-label">商品名称</label>
        <div class="layui-input-block">
          <input type="text" name="itemName" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <!-- 商品价格 -->
      <div class="layui-form-item">
        <label class="layui-form-label">商品价格</label>
        <div class="layui-input-block">
          <input type="text" name="itemPrice" required lay-verify="required" placeholder="请输入商品价格" autocomplete="off"
            class="layui-input">
        </div>
      </div>
      <!-- 商品库存 -->
      <div class="layui-form-item">
        <label class="layui-form-label">商品库存</label>
        <div class="layui-input-inline">
          <input type="number" name="itemStore" placeholder="请输入商品库存" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-form-mid layui-word-aux">请输入纯数字</div>
      </div>
      <!-- 商品封面 -->
      <div class="layui-form-item">
        <label class="layui-form-label">商品封面</label>
        <div class="layui-input-inline">
          <button type="button" class="layui-btn" id="upload">
            <i class="layui-icon">&#xe67c;</i>上传图片
          </button>
        </div>
        <img alt="" width="150" height="auto" id="img" src="">
        
      </div>
      <!-- 商品状态 -->
      <div class="layui-form-item" pane>
        <label class="layui-form-label">商品状态</label>
        <div class="layui-input-block">
          <input type="checkbox" name="isSale" lay-skin="switch" lay-text="上架|下架" checked>
        </div>
      </div>
      <!-- 商品简介 -->
      <div class="layui-form-item layui-form-text" pane>
        <label class="layui-form-label">商品简介</label>
        <div class="layui-input-block">
          <textarea name="itemIntro" placeholder="请输入内容" class="layui-textarea"></textarea>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="layui-form-item layui-form-text" pane>
        <label class="layui-form-label">商品详情</label>
        <div class="layui-input-block">
          <!-- 引入富文本编辑器 -->
          <div class="richText"></div>
        </div>
      </div>
      <!-- 表单提交 -->
      <div class="layui-form-item">
        <div class="layui-input-block">
          <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
          <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
      </div>
    </form>
  </div>
  <script src="/static/layui/layui.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/wangeditor@latest/dist/wangEditor.min.js"></script>
  <script>
    layui.use(['form', 'jquery', 'upload', 'layer'], function () {
      //实例化表单
      var form = layui.form;
      //实例化上传
      var upload = layui.upload;
      //实例化jquery
      var $ = layui.jquery;
      //存储图片路径
      let thumb = '';
      //upload实例
      var uploadInst = upload.render({
        elem: '#upload',   //绑定元素
        url: '/upload/',   //上传接口
        field: 'file',
        done: function (res) {
          //上传成功提示
          layer.msg(res.msg);
          console.log(res.data.url);
          //显示上传预览图
          $('#img').attr('src', res.data.url);
          thumb = res.data.url;
        },
        error: function (res) {
        }
      });

      //实例化富文本对象
      const E = window.wangEditor;
      const editor = new E(".richText");
      editor.create();
      //获取富文本的内容
      // editor.txt.text();
      
      //监听提交
      form.on('submit(formDemo)', function (data) {
        data.field.content = editor.txt.text();
        data.field.itemThumb = thumb;
        //对上架状态数据进行处理
        data.field.isSale === 'on' ? data.field.isSale = '0' : data.field.isSale = '1';
        console.log(data.field);
        $.ajax({
          type: 'post',
          url: '/additem',
          data: data.field,
          success: (res)=>{
            console.log(res);
          }
        })
      });
    });
  </script>


</body>

</html>